package smartbooks.ui.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import smartbooks.exceptions.MyException;
import smartbooks.ui.SmartBooksUserSettings;
import smartbooks.ui.helper.ItemHelper;
import smartbooks.ui.util.CommonConstants;
import smartbooksdb.exception.ItemException;
import smartbooksdb.exception.MandatoryFieldException;
import smartbooksdb.hibernatebean.Item;
import smartbooksdb.service.ItemService;

/**
 *
 * @author Ankit
 */
public class ItemManager implements CommonConstants {
    /**
     * This method is used to create item
     * @param item
     * @return 'true' if the item is created or else 'false'
     * @throws MyException
     * @throws MandatoryFieldException
     */
    public static boolean createItem(Item item) throws MyException, MandatoryFieldException {
        try {
            System.out.println(item.toString());
            ItemService itemService = new ItemService();
            
            boolean itemPanelValidated = ItemHelper.validateItem(item);

            if(!itemService.isItemNameAvailable(item.getItemName(), true)) {
                throw new MyException(ITEM_NAME, "Item name unavailable.");
            }
            
            if(itemPanelValidated) {
                long itemId = itemService.createItem(item);
                
                return true;
            }
        } catch (ItemException ex) {
            Logger.getLogger(ItemManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }
    
    /**
     * This method is used to update item
     * @param item
     * @param editItem
     * @return 'true' if the item is updated or else 'false'
     * @throws MyException
     * @throws MandatoryFieldException
     */
    public static boolean updateItem(Item item, Item editItem) throws MyException, MandatoryFieldException {
        try {
            System.out.println(item.toString());
            ItemService itemService = new ItemService();
            
            boolean itemPanelValidated = ItemHelper.validateItem(item);
            
            if(!item.getItemName().equals(editItem.getItemName())) {
                if(!itemService.isItemNameAvailable(item.getItemName(), true)) {
                    throw new MyException(ITEM_NAME, "Item name unavailable.");
                }
            }
            
            if(itemPanelValidated) {
                itemService.updateItem(item);
                
                return true;
            }
        } catch (ItemException ex) {
            Logger.getLogger(ItemManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }
    
    /**
     * This method is used to delete item by updating its status to inactive
     * @param item
     * @return 'true' if the item is deleted or else 'false'
     */
    public static boolean deleteItem(Item item) {
        try {
            ItemService itemService = new ItemService();
            
            item.setUpdatedBy(SmartBooksUserSettings.USERNAME);
            item.setStatus(INACTIVE);
            itemService.updateItem(item);
            
            return true;
        } catch (ItemException | MandatoryFieldException ex) {
            Logger.getLogger(ItemManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }
    
    /**
     * This method is used to get all active items
     * @return list of all active items
     */
    public static ArrayList<Item> getAllActiveItems() {
        ItemService itemService = new ItemService();        
        List<Item> items = itemService.getAllActiveItems();
        
        return (ArrayList<Item>) items;
    }
}